package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.utils.Androids;

/**
 * 安全中心
 */
public class SecurityCenterViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();
    private final String region = Androids.LocalConfig.getRegion();

    private final MutableLiveData<Resource<BaseResponseModel>> mSendMsmLiveData;
    private final MutableLiveData<Resource<BaseResponseModel>> mModifyLoginPasswordLiveData;
    private final MutableLiveData<Resource<BaseResponseModel>> mTransactionPasswordLiveData;

    public SecurityCenterViewModel(@NonNull Application application) {
        super(application);
        mSendMsmLiveData = new MutableLiveData<>();
        mModifyLoginPasswordLiveData = new MutableLiveData<>();
        mTransactionPasswordLiveData = new MutableLiveData<>();
    }

    /**
     * 发送短信
     */
    public MutableLiveData<Resource<BaseResponseModel>> sendMessage(int type) {
        String mobilePhone = Androids.LocalConfig.getPhone();
        mSendMsmLiveData.setValue(Resource.loading());
        DataRepository.get().sendMessage(region, mobilePhone, type, result -> {
            if (result.isSucceed()) {
                mSendMsmLiveData.setValue(Resource.success(result, result.getMsg()));
            } else {
                mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mSendMsmLiveData;
    }

    /**
     * 修改登录密码
     */
    public MutableLiveData<Resource<BaseResponseModel>> modifyLoginPassword(
            String oldPassword, String newPassword, String confirmPassword, String code) {

        if (VerificationManage.verifyPassword(oldPassword)
                && VerificationManage.verifyPasswordSame(newPassword, confirmPassword)) {

            mModifyLoginPasswordLiveData.setValue(Resource.loading());
            DataRepository.get().modifyLoginPassword(token, uid, oldPassword, newPassword,
                    confirmPassword, code, result -> {
                        if (result.isSucceed()) {
                            mModifyLoginPasswordLiveData.setValue(Resource.success(result, result.getMsg()));
                        } else {
                            mModifyLoginPasswordLiveData.setValue(Resource.error(result.getMsg()));
                        }
                    });
        }

        return mModifyLoginPasswordLiveData;
    }

    /**
     * 设置或修改交易密码
     */
    public MutableLiveData<Resource<BaseResponseModel>> setOrModifyTransactionPassword(
            String code, String password, String confirmPassword) {

        if (VerificationManage.verifyMessageVerifyCode(code)
                && VerificationManage.verifyPasswordSame(password, confirmPassword)) {
            mTransactionPasswordLiveData.setValue(Resource.loading());
            DataRepository.get().modifyTransactionPassword(token, uid, password, confirmPassword, code,
                    result -> {
                        if (result.isSucceed()) {
                            mTransactionPasswordLiveData.setValue(Resource.success(result, result.getMsg()));
                        } else {
                            mTransactionPasswordLiveData.setValue(Resource.error(result.getMsg()));
                        }
                    });
        }

        return mTransactionPasswordLiveData;
    }
}
